package jun.learn.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import jun.work.io.StreamUtils;

public class URLTest {
	public static void main(String[] args) {
		String uriStr = "http://www.baidu.com/index.html";
		try {
			URI uri = new URI(uriStr);
			System.out.println(uri.getAuthority());
			System.out.println(uri.getHost());
			System.out.println(uri.getPort());
			System.out.println(uri.getPath());
			System.out.println(uri.getRawPath());
		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}
		try {
			URL url = new URL("http", "news.baidu.com", -1, "/z/ljjh/zhuanti.html");
//			URL url = new URL("http://www.baidu.com");
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");
			conn.setReadTimeout(50000);
			conn.connect();
			if(conn.getResponseCode() == 200){
				InputStream is = conn.getInputStream();
				System.out.println(StreamUtils.getStringByIs(is, "GBK"));
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
